package com.baihan.angus.app.game.basic.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baihan.angus.app.game.basic.R;
import com.baihan.angus.app.game.basic.utils.ComUtil;

import static android.util.TypedValue.COMPLEX_UNIT_SP;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.widget.ListPopupWindow.MATCH_PARENT;

public class TitleView extends FrameLayout {
    private String leftText;
    private String titleText;
    private int titleTextColor;
    private int leftTextColor;
    private int rightTextColor;
    private boolean horLine;
    private Drawable leftIcon;
    private Drawable rightIcon;

    private LinearLayout llLeft;
    private LinearLayout llRight;
    private TextView tvTitle;
    private String rightText;
    private ImageView ivRightIcon;
    private TextView tvRight;
    private ImageView ivLeftIcon;
    private TextView tvLeft;

    public TitleView(Context context) {
        super(context);
        init(context);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        readAttrs(context, attrs);
        init(context);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        readAttrs(context, attrs);
        init(context);
    }

    private void readAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        leftText = typedArray.getString(R.styleable.TitleView_leftText);
        titleText = typedArray.getString(R.styleable.TitleView_titleText);
        titleTextColor = typedArray.getColor(R.styleable.TitleView_titleTextColor, context.getColor(R.color.bg4));
        leftTextColor = typedArray.getColor(R.styleable.TitleView_leftTextColor, context.getColor(R.color.bg4));
        rightTextColor = typedArray.getColor(R.styleable.TitleView_rightTextColor, context.getColor(R.color.bg_black));
        leftIcon = typedArray.getDrawable(R.styleable.TitleView_leftIcon);
        rightIcon = typedArray.getDrawable(R.styleable.TitleView_rightIcon);
        rightText = typedArray.getString(R.styleable.TitleView_rightText);
        horLine = typedArray.getBoolean(R.styleable.TitleView_horLine, false);
        typedArray.recycle();
    }

    private void init(Context context) {
        //Left
        llLeft = new LinearLayout(context);
        LayoutParams llLeftLayoutParams = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        llLeftLayoutParams.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
        llLeftLayoutParams.leftMargin = ComUtil.Dp2Px(context, 16);
        llLeft.setLayoutParams(llLeftLayoutParams);

        LinearLayout.LayoutParams tvLeftP = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        tvLeftP.gravity = Gravity.CENTER_VERTICAL;
        tvLeft = new TextView(context);
        if (!TextUtils.isEmpty(leftText)) {
            tvLeft.setText(leftText);
        }
        tvLeft.setTextColor(leftTextColor);
        tvLeft.setTextSize(COMPLEX_UNIT_SP, 14);
        llLeft.addView(tvLeft, tvLeftP);

        ivLeftIcon = new ImageView(context);
        if (leftIcon != null) {
            ivLeftIcon.setImageDrawable(leftIcon);
            llLeft.addView(ivLeftIcon, ComUtil.Dp2Px(context, 24), ComUtil.Dp2Px(context, 24));
        }
        this.addView(llLeft);


        //Right
        llRight = new LinearLayout(context);
        LayoutParams llRightLayoutParams = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        llRightLayoutParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
        llRightLayoutParams.rightMargin = ComUtil.Dp2Px(context, 16);
        llRight.setLayoutParams(llRightLayoutParams);

        LinearLayout.LayoutParams tvRightP = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        tvRightP.gravity = Gravity.CENTER_VERTICAL;
        tvRight = new TextView(context);
        if (!TextUtils.isEmpty(rightText)) {
            tvRight.setText(rightText);
        }
        tvRight.setTextColor(rightTextColor);
        tvRight.setTextSize(COMPLEX_UNIT_SP, 16);
        tvRight.getPaint().setFakeBoldText(true);
        llRight.addView(tvRight, tvRightP);

        ivRightIcon = new ImageView(context);
        if (rightIcon != null) {
            ivRightIcon.setImageDrawable(rightIcon);
            llRight.addView(ivRightIcon, ComUtil.Dp2Px(context, 24), ComUtil.Dp2Px(context, 24));
        }

        this.addView(llRight);

        //Title
        LayoutParams tvTitleLayoutParams = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        tvTitleLayoutParams.gravity = Gravity.CENTER;
        tvTitle = new TextView(context);
        tvTitle.setTextColor(titleTextColor);
        tvTitle.setTextSize(COMPLEX_UNIT_SP, 16);
        tvTitle.getPaint().setFakeBoldText(true);
        this.addView(tvTitle, tvTitleLayoutParams);
        if (!TextUtils.isEmpty(titleText)) {
            tvTitle.setText(titleText);
            tvTitle.setTextColor(titleTextColor);
        }

        //分割线
        if (horLine) {
            LayoutParams lineLayoutParams = new LayoutParams(MATCH_PARENT, 1);
            lineLayoutParams.gravity = Gravity.BOTTOM;
            View line = new View(context);
            line.setBackgroundColor(context.getColor(R.color.lineColor1));
            this.addView(line, lineLayoutParams);
        }
    }

    public void setTitleText(String title) {
        titleText = title;
        tvTitle.setText(title);
    }

    public void setLeftText(String leftText) {
        this.leftText = leftText;
        tvLeft.setText(leftText);
    }

    public void setRightText(String rightText) {
        this.rightText = rightText;
        tvRight.setText(rightText);
    }

    /**
     * 设置左侧点击事件
     */
    public void setLeftOnClickListener(OnClickListener onClickListener) {
        if (llLeft != null) {
            llLeft.setOnClickListener(onClickListener);
        }
    }

    /**
     * 设置右侧点击事件
     */
    public void setRightOnClickListener(OnClickListener onClickListener) {
        if (llRight != null) {
            llRight.setOnClickListener(onClickListener);
        }
    }

}
